package com.lcxw.hxzbadmin.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.GiftSend;
import com.lcxw.hxzbapi.service.GiftSendService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.GiftService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.StringUtil;

@RestController
@CrossOrigin
@RequestMapping("admin/gift/earn")
public class GiftEarnController {

    private Logger logger = LoggerFactory.getLogger(GiftEarnController.class);

    @Resource
    GiftSendService giftSendService;

    @Resource
    YwpdRoomService roomService;

    @Resource
    GiftService giftService;

    @PostMapping("/add")
    public Response addGiftEarn(@RequestBody JSONObject requestParams) {
        long fromUserId = Long.parseLong(requestParams.getString("fromUserId"));
        long roomId = Long.parseLong(requestParams.getString("roomId"));
        int giftId = Integer.parseInt(requestParams.getString("giftId"));
        int giftNum = Integer.parseInt(requestParams.getString("giftNum"));
        logger.info("room {} earn a gift {},num is {}", roomId, giftId, giftNum);

        GiftSend giftSend = new GiftSend();

        try {
                long userId = roomService.selectRoomByRoomId(roomId).getUserId();
                int needCoin = giftService.selectByPrimaryKey(giftId).getNeedCoin();
                giftSend.setUserId(userId);
                giftSend.setGiftId(giftId);
                giftSend.setFromUserId(fromUserId);
                giftSend.setGiftNum(giftNum);
                giftSend.setNeedCoin(needCoin);
                giftSend.setAddTime(StringUtil.getDateTime());
                giftSendService.insertGiftEarn(giftSend);
        } catch (Exception e) {
            return Response.dbFail(logger, "db operate");
        }

        return Response.common(logger, "add giftEarn success.", 0, "success", null);

    }

}
